<?php
declare (strict_types=1);

namespace app\controller;

use app\model\DeamxFoodOrder;
use think\facade\Request;

/**
 * ## 处理支付后的接口
 */
class Payment
{
    public function afterPayment()
    {
        try {
            $order_id = Request::post('order_id');
            $orderInfo = DeamxFoodOrder::find($order_id);
            $msg = "订单编号：" . $orderInfo->ordersn . "\n";

            $goodsList = json_decode($orderInfo->goods_list, true);
            $goodsName = [];
            for ($i = 0; $i < count($goodsList); $i++) {
                if ($goodsList[$i]['count'] > 0) {
                    $goods_name = $goodsList[$i]['name'];
                    for ($j = 0; $j < count($goodsList[$i]['options']); $j++) {
                        $goodsName[] = $goods_name . "-" . $goodsList[$i]['options'][$j]['name'] . "  x" . $goodsList[$i]['options'][$j]['count'];
                    }
                }
            }
            $msg .= "\n商品列表:\n" . implode("\n", $goodsName);
            $msg .= "\n\n订单类型:";
            if ($orderInfo->order_type == 1) {
                $msg .= "【堂食 " . $orderInfo->order_number . "】";
            }
            if ($orderInfo->order_type == 2) {
                $msg .= "【外卖】\n";
                $msg .= "\n配送信息：\n";
                $addressInfo = json_decode($orderInfo->address_info, true);
                $address = [
                    "姓名：" . $addressInfo['realname'],
                    "手机号：" . $addressInfo['telphone'],
                    "地址：" . $addressInfo['address_road'] . $addressInfo['number'],
                ];
                $msg .= implode("\n", $address);
            }
            if ($orderInfo->order_type == 3) {
                $msg .= "【自提 " . $orderInfo->order_number . "】";
            }

            $workWechat = new WorkWechat();
            $result = $workWechat->sendMessage($msg);
            return responseData(true, '推送成功');
        } catch (\Exception $e) {
//            selfLog(LOG_LEVEL_ERROR, "Payment", $e);
            return responseData(false, $e->getMessage(), $e->getFile() . ":" . $e->getLine());
        }
    }
}
